package com.llg.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llg.mapper.SendSpecMapper;
import com.llg.model.code.CommonCode;
import com.llg.model.code.PageCode;
import com.llg.model.response.DataResult;
import com.llg.model.response.PageResult;
import com.llg.model.response.ResponseResult;
import com.llg.pojo.SendSpec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class SendSpecService {

    @Autowired
    private SendSpecMapper mapper;

    //通过品牌id查询所有发送卡
    public PageResult<SendSpec> findByBId(int bid){
        Example example = new Example(SendSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("bid", bid);
        List<SendSpec> list = mapper.selectByExample(example);
        return new PageResult<>(CommonCode.SUCCESS, list.size(), 1, list);
    }

    //搜索并分页查询
    public PageResult<SendSpec> findList(int currentPage,int pageSize,SendSpec sendSpec){
        //参数判断
        if(currentPage <= 0) return new PageResult<>(PageCode.PAGE_ERR1, 0, 0, null);
        if(pageSize <= 0) return new PageResult<>(PageCode.SIZE_ERR, 0, 0, null);
        PageHelper.startPage(currentPage, pageSize);
        Example example = new Example(SendSpec.class);
        Example.Criteria criteria = example.createCriteria();
        if (sendSpec != null && !sendSpec.getName().equals("")) {
            criteria.andLike("name", "%" + sendSpec.getName() + "%");
        }
        List<SendSpec> list = mapper.selectByExample(example);
        PageInfo<SendSpec> page = new PageInfo<>(list);
        return new PageResult<>(CommonCode.SUCCESS, (int) page.getTotal(), page.getPages(), page.getList());
    }

    //通过id查
    public DataResult<SendSpec> findById(int id){
        return new DataResult<>(CommonCode.SUCCESS, mapper.selectByPrimaryKey(id));
    }

    //增
    public DataResult<SendSpec> add(SendSpec sendSpec){
        mapper.insertSelective(sendSpec);
        return new DataResult<>(CommonCode.SUCCESS, sendSpec);
    }

    //删
    public ResponseResult delete(int id){
        mapper.deleteByPrimaryKey(id);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //该
    public ResponseResult update(SendSpec sendSpec){
        mapper.updateByPrimaryKeySelective(sendSpec);
        return new ResponseResult(CommonCode.SUCCESS);
    }
}
